﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using LPrisaMVC.Entities;
using LPrisaMVC.BusinessLogic;
using Microsoft.Practices.Unity;
using Microsoft.Practices.Unity.Configuration;

namespace LPrisaMVC.WebMVC4.Controllers
{
    public class CreditoController : Controller
    {
        //
        // GET: /Credito/

        [Dependency]
        public ICreditoBL creditoBL { get; set; }

        [Dependency]
        public IPagoBL pagoBL { get; set; }

        [Authorize(Roles = "admin, ventas")]
        public ActionResult Index()
        {
            var creditos = creditoBL.GetAllFromCreditos();
            return View(creditos);
        }

        [HttpPost]
        [Authorize(Roles = "admin, ventas")]
        public ActionResult Index(string criterio = "", string fec1 = "", string fec2 = "", bool cliente = false, bool fechas = false, bool ambos = false)
        {
            try
            {
                var fecha1 = new DateTime();
                var fecha2 = new DateTime();

                if (fec1 == "" && fec2 == "")
                {
                    fecha1 = DateTime.Now.Date;
                    fecha2 = DateTime.Now.Date;
                }
                else
                {
                    fecha1 = Convert.ToDateTime(fec1);
                    fecha2 = Convert.ToDateTime(fec2);
                }

                var returnCreditos = new List<CREDITO>();
                if (cliente == true)
                {
                    var creditos = creditoBL.GetFromCreditobyCliente(criterio);
                    returnCreditos = creditos;
                }
                if (fechas == true)
                {
                    var creditos = creditoBL.GetFromCreditobyFechaAndCliente(fecha1, fecha2, "");
                    returnCreditos = creditos;
                }
                if (ambos == true)
                {
                    var creditos = creditoBL.GetFromCreditobyFechaAndCliente(fecha1, fecha2, criterio);
                    returnCreditos = creditos;
                }
                return View(returnCreditos);
            }
            catch 
            {
                var creditos = creditoBL.GetAllFromCreditos();
                return View(creditos);
            }
        }

        [Authorize(Roles = "admin, ventas")]
        public ActionResult Pagos(int id = 0)
        {
            if (id == 0)
            {
                return RedirectToAction("Index", "Credito");
            }

            var credito = creditoBL.GetFromCreditoById(id);

            if (credito == null)
            {
                return RedirectToAction("Index", "Credito");
            }

            ViewData["CreditoId"] = credito.Id;
            ViewData["CLIENTE"] = credito.CLIENTE.NombreRazonSocial;
            ViewData["DNIRUC"] = credito.CLIENTE.DniRuc;
            if (credito.DETALLEPAGOes.Count > 0)
            {
                ViewData["FechaUltPago"] = credito.DETALLEPAGOes.Last().Fecha.ToShortDateString();
                ViewData["MontoUltPago"] = credito.DETALLEPAGOes.Last().Monto.ToString();
            }
            else
            {
                ViewData["FechaUltPago"] = "";
                ViewData["MontoUltPago"] = "";
            }
            ViewData["TODEUDA"] = credito.Total;
            var fechaCredito = credito.FechaCredito.Date;
            ViewData["VENCIMIENTO"] = fechaCredito.AddDays(30).ToLongDateString();

            return View(credito);
        }

        [HttpPost]
        [Authorize(Roles = "admin, ventas")]
        public ActionResult Pagos(int id, string montoPagar = "")
        {
            try
            {
                DETALLEPAGO pago = new  DETALLEPAGO()
                {
                    CreditoId = id,
                    Descripcion = "Pago hecho a las : " + Convert.ToString(DateTime.Now.ToLongTimeString()),
                    Fecha = DateTime.Today.Date, 
                    Monto = Convert.ToDecimal(montoPagar.ToString()), 
                };
                pagoBL.AddPago(pago); 
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
    }
}
